package controller.update;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.db.MyDBAccess;
/**
 * Servlet implementation class Update_Completion
 */
@WebServlet("/UpdateCompletion")
public class UpdateCompletion extends HttpServlet {
	private static final long serialVersionUID = 1L;
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UpdateCompletion() {
		super();
		// TODO Auto-generated constructor stub
	}
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String employee_no = request.getParameter("employee_no");
		String check_emp_no = "";
		String first_name = request.getParameter("first_name");
		String last_name = request.getParameter("last_name");
		String sex = request.getParameter("sex");
		String join_date = request.getParameter("join_date");
		String section = request.getParameter("section");
		String mail = request.getParameter("mail");
		if(sex.equals("男性")){
			sex = "1";
		}
		if(sex.equals("女性")){
			sex = "2";
		}
		//空欄チェック
		if("".equals(first_name) || "".equals(last_name)|| "".equals(sex) || "".equals(join_date) || "".equals(section)){
			ServletContext sc = getServletContext();
			RequestDispatcher rd = sc.getRequestDispatcher("/InputErr.jsp");
			//forward
			rd.forward(request, response);
			System.out.println("空欄有");
			return;
		}
		//文字数チェック
		if(employee_no.length() != 5 || first_name.length() > 32 || last_name.length() > 32 || sex.length() != 1 || join_date.length() != 8 || section.length() != 3 || mail.length() > 256){
			ServletContext sc = getServletContext();
			RequestDispatcher rd = sc.getRequestDispatcher("/InputErr.jsp");
			//forward
			rd.forward(request, response);
			System.out.println("文字数不正");
			return;
		}
		//入社年月日の形式チェック（全角チェック）
		byte[] check_join_date = join_date.getBytes("SJIS");
		if(join_date.length() != check_join_date.length){
			ServletContext sc = getServletContext();
			RequestDispatcher rd = sc.getRequestDispatcher("/InputErr.jsp");
			//forward
			rd.forward(request, response);
			System.out.println("入社年月日、形式不正（全角）");
			return;
		}
		//実在日チェック
		DateFormat format = DateFormat.getDateInstance();
		format.setLenient(false);
		StringBuilder sb = new StringBuilder();
		sb.append(join_date);
		sb.insert(4, "/");
		sb.insert(7, "/");
		try {
			format.parse(new String(sb));
		} catch (Exception e) {
			ServletContext sc = getServletContext();
			RequestDispatcher rd = sc.getRequestDispatcher("/InputErr.jsp");
			//forward
			rd.forward(request, response);
			System.out.println("入社年月日不正");
			return;
		}
		//入社年月日の形式チェック
		if("0".equals(join_date.substring(0, 1))){
			ServletContext sc = getServletContext();
			RequestDispatcher rd = sc.getRequestDispatcher("/InputErr.jsp");
			//forward
			rd.forward(request, response);
			System.out.println("入社年月日、形式不正");
			return;
		}
		// 内容: データベースにアクセスする
		// MyDBAccess のインスタンスを生成する
		MyDBAccess db = new MyDBAccess();
		// データベースへのアクセス
		try {
			db.open();
		} catch (Exception e) {
			// TODO 自動生成された catch ブロック
			e.printStackTrace();
		}
		String sql_select = "select employee_no from employee_info where employee_no = \'" + employee_no + "\';";
		ResultSet rs = null;
		try {
			rs = db.getResultSet(sql_select);
		} catch (Exception e2) {
			// TODO 自動生成された catch ブロック
			e2.printStackTrace();
		}
		try {
			while(rs.next()) {
				check_emp_no = rs.getString("employee_no");
			}
		} catch (SQLException e2) {
			// TODO 自動生成された catch ブロック
			e2.printStackTrace();
		}
		//社員番号存在チェック
		if(employee_no.equals(check_emp_no) == false){
			ServletContext sc = getServletContext();
			RequestDispatcher rd = sc.getRequestDispatcher("/ErrSameEmployeeNo.jsp");
			//forward
			rd.forward(request, response);
			System.out.println("社員番号存在なし");
			return;
		}
		String sql = "update employee_info set first_name=\'" + first_name + "\',"
				+ "last_name=\'" + last_name + "\',"
				+ "sex=\'" + sex + "\',"
				+ "join_date=\'" + join_date + "\',"
				+ "section=\'" + section + "\',"
				+ "mail=\'" + mail + "\'"
				+ "where employee_no=\'" + employee_no + "\';";
		try {
			db.execute(sql);
		} catch (Exception e1) {

			e1.printStackTrace();
		}
		try {
			db.close();
		} catch (Exception e) {
			// TODO 自動生成された catch ブロック
			e.printStackTrace();
		}
		request.setCharacterEncoding("UTF-8");
		//RequestDispatcher
		ServletContext sc = getServletContext();
		RequestDispatcher rd = sc.getRequestDispatcher("/UpdateCompletion.jsp");
		//forward
		rd.forward(request, response);
	}
}